/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.controller;

import com.minierp.bean.ClieUserBean;
import com.minierp.bean.EstablishmentBean;
import com.minierp.bean.EstablishmentContactBean;
import com.minierp.dao.AreaDao;
import com.minierp.dao.EstablishmentContactDao;
import com.minierp.dao.EstablishmentDao;
import com.minierp.dao.IdGenerator;
import com.minierp.helper.EncCommonHelper;
import com.minierp.model.Establishment;
import com.minierp.model.EstablishmentContact;
import com.minierp.model.EstablishmentLog;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class ClieFormCtr extends SimpleFormController {
	private AreaDao areaDao;
	private EstablishmentContactDao establishmentContactDao;
	private EstablishmentDao establishmentDao;
	private IdGenerator idGenerator;

	protected Map referenceData(HttpServletRequest request) throws Exception {
		HashMap refData = new HashMap();
		refData.put("areaList", this.areaDao.getList(true));
		return refData;
	}

	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		ClieUserBean ub = EncCommonHelper.getClieUserAccount(request);
		EstablishmentBean form = new EstablishmentBean();
		ArrayList arr = new ArrayList();
		Establishment establishment = (Establishment) this.establishmentDao
				.get(Establishment.class, ub.getUser().getClieId());
		form.setEstablishment(establishment);
		if (establishment == null) {
			throw new Exception("Record not found for id:"
					+ ub.getUser().getClieId());
		} else {
			List contactList = this.establishmentContactDao
					.getList(establishment.getId());

			for (int i = 0; i < contactList.size(); ++i) {
				EstablishmentContactBean cb = new EstablishmentContactBean();
				EstablishmentContact c = (EstablishmentContact) contactList
						.get(i);
				cb.setEstablishmentContact(c);
				cb.setStatus("UNCHANGE");
				cb.setOldId(c.getId());
				arr.add(cb);
			}

			form.setContacts(arr);
			return form;
		}
	}

	protected void onBind(HttpServletRequest request, Object command)
			throws Exception {
		EstablishmentBean form = (EstablishmentBean) command;
		int num = Integer.parseInt(request.getParameter("contact_num"));
		ArrayList arr = new ArrayList();
		boolean foundFirstContact = false;

		for (int i = 0; i < num; ++i) {
			EstablishmentContactBean cb = new EstablishmentContactBean();
			EstablishmentContact c = new EstablishmentContact();
			c.setContactName(request.getParameter("contact_name_" + i));
			c.setEmail(request.getParameter("email_" + i));
			c.setIm(request.getParameter("im_" + i));
			c.setMark(request.getParameter("mark_" + i));
			c.setMobile(request.getParameter("mobile_" + i));
			c.setPhone(request.getParameter("phone_" + i));
			c.setPossie(request.getParameter("possie_" + i));
			c.setSex(request.getParameter("sex_" + i));
			if (request.getParameter("is_main_" + i) != null
					&& request.getParameter("is_main_" + i).equals("1")) {
				c.setIsMain("Y");
			} else {
				c.setIsMain("N");
			}

			if (request.getParameter("id_" + i) != null
					&& !request.getParameter("id_" + i).equals("")) {
				c.setId(Long.parseLong(request.getParameter("id_" + i)));
				cb.setOldId(c.getId());
			}

			cb.setEstablishmentContact(c);
			cb.setStatus(request.getParameter("status_" + i));
			arr.add(cb);
			if (!foundFirstContact && !cb.getStatus().equals("DELETE")
					&& c.getContactName() != null
					&& !c.getContactName().equals("")) {
				form.getEstablishment().setLinkman(c.getContactName());
				form.getEstablishment().setLinkmanPhone(c.getMobile());
				foundFirstContact = true;
			}
		}

		if (!foundFirstContact) {
			form.getEstablishment().setLinkman("");
			form.getEstablishment().setLinkmanPhone("");
		}

		form.setContacts(arr);
	}

	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		String alertMsg = "";
		EstablishmentBean form = (EstablishmentBean) command;
		Establishment obj = form.getEstablishment();
		boolean estChange = false;
		EstablishmentLog log = new EstablishmentLog();
		new ArrayList();
		Object[] objs = this.establishmentDao.getStatusValues(obj.getId());
		String oName = "";
		String oPhone = "";
		String oEmail = "";
		String oAddress = "";
		String nName = "";
		String nPhone = "";
		String nEmail = "";
		String nAddress = "";
		if (objs[0] != null) {
			oName = objs[0].toString();
		}

		if (objs[1] != null) {
			oPhone = objs[1].toString();
		}

		if (objs[2] != null) {
			oEmail = objs[2].toString();
		}

		if (objs[3] != null) {
			oAddress = objs[3].toString();
		}

		if (obj.getEstName() != null) {
			nName = obj.getEstName();
		}

		if (obj.getPhone() != null) {
			nPhone = obj.getPhone();
		}

		if (obj.getEmail() != null) {
			nEmail = obj.getEmail();
		}

		if (obj.getAddress() != null) {
			nAddress = obj.getAddress();
		}

		if (!oName.equals(nName)) {
			log.setEstName(oName + " --> " + nName);
			estChange = true;
		}

		if (!oPhone.equals(nPhone)) {
			log.setPhone(oPhone + " --> " + nPhone);
			estChange = true;
		}

		if (!oEmail.equals(nEmail)) {
			log.setEmail(oEmail + " --> " + nEmail);
			estChange = true;
		}

		if (!oAddress.equals(nAddress)) {
			log.setAddress(oAddress + " --> " + nAddress);
			estChange = true;
		}

		List oldContList = this.establishmentContactDao.getList(obj.getId());
		this.establishmentDao.update(obj);
		alertMsg = "修改成功！";
		String conChangeStr = this.saveContacts(obj.getId(),
				form.getContacts(), oldContList);
		if (!conChangeStr.equals("")) {
			log.setContact(conChangeStr);
			estChange = true;
		}

		if (estChange) {
			String mv = obj.getEstName();
			if (mv.length() > 15) {
				mv = mv.substring(0, 15);
			}

			log.setId(this.idGenerator.getId(EstablishmentLog.class.getName()));
			log.setEstId(obj.getId());
			log.setModifierId(0L);
			log.setModifierName(mv);
			this.establishmentDao.save(log);
		}

		ModelAndView mv1 = new ModelAndView("redirect:/online/ClieEdit.ihtm");
		EncCommonHelper.setAlertMessage(request, alertMsg);
		return mv1;
	}

	private String saveContacts(long estId, List contacts, List oldContacts) {
		String changeStr = "";

		for (int i = 0; i < contacts.size(); ++i) {
			EstablishmentContactBean bean = (EstablishmentContactBean) contacts
					.get(i);
			bean.getEstablishmentContact().setEstId(estId);
			if (bean.getStatus().equals("DELETE")) {
				if (bean.getEstablishmentContact().getId() != 0L) {
					this.establishmentContactDao.delete(bean
							.getEstablishmentContact().getId());
				}
			} else if (!bean.getStatus().equals("EDIT")) {
				if (bean.getStatus().equals("NEW")) {
					bean.getEstablishmentContact().setId(
							this.idGenerator.getId(EstablishmentContact.class
									.getName()));
					this.establishmentContactDao.save(bean
							.getEstablishmentContact());
				}
			} else {
				for (int j = 0; j < oldContacts.size(); ++j) {
					EstablishmentContact oc = (EstablishmentContact) oldContacts
							.get(j);
					if (oc.getId() == bean.getEstablishmentContact().getId()) {
						String oName = "";
						String nName = "";
						String oPhone = "";
						String nPhone = "";
						String oMobile = "";
						String nMobile = "";
						if (oc.getContactName() != null) {
							oName = oc.getContactName();
						}

						if (bean.getEstablishmentContact().getContactName() != null) {
							nName = bean.getEstablishmentContact()
									.getContactName();
						}

						if (oc.getPhone() != null) {
							oPhone = oc.getPhone();
						}

						if (bean.getEstablishmentContact().getPhone() != null) {
							nPhone = bean.getEstablishmentContact().getPhone();
						}

						if (oc.getMobile() != null) {
							oMobile = oc.getMobile();
						}

						if (bean.getEstablishmentContact().getMobile() != null) {
							nMobile = bean.getEstablishmentContact()
									.getMobile();
						}

						if (!oName.equals(nName) || !oPhone.equals(nPhone)
								|| !oMobile.equals(nMobile)) {
							if (!changeStr.equals("")) {
								changeStr = changeStr + "\n";
							}

							changeStr = changeStr + oName + " " + oPhone + " "
									+ oMobile + " --> " + nName + " " + nPhone
									+ " " + nMobile;
						}
						break;
					}
				}

				this.establishmentContactDao.update(bean
						.getEstablishmentContact());
			}
		}

		return changeStr;
	}

	public IdGenerator getIdGenerator() {
		return this.idGenerator;
	}

	public void setIdGenerator(IdGenerator idGenerator) {
		this.idGenerator = idGenerator;
	}

	public EstablishmentDao getEstablishmentDao() {
		return this.establishmentDao;
	}

	public void setEstablishmentDao(EstablishmentDao establishmentDao) {
		this.establishmentDao = establishmentDao;
	}

	public AreaDao getAreaDao() {
		return this.areaDao;
	}

	public void setAreaDao(AreaDao areaDao) {
		this.areaDao = areaDao;
	}

	public EstablishmentContactDao getEstablishmentContactDao() {
		return this.establishmentContactDao;
	}

	public void setEstablishmentContactDao(
			EstablishmentContactDao establishmentContactDao) {
		this.establishmentContactDao = establishmentContactDao;
	}
}